<template>
  <div class="login-wrapper">
    <el-card class="login-card">
      <div class="login-content">
        <div class="login-left">
          <img src="@/assets/images/left-picture.png" alt="" class="login-image" />
        </div>
        <div class="login-right">
          <h1 class="login-title">全面预算管理系统</h1>
          <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
            <el-form-item prop="username">
              <el-input v-model="loginForm.username" placeholder="请输入用户名" :prefix-icon="User" size="large" />
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" :prefix-icon="Lock" show-password size="large" />
            </el-form-item>
            <el-form-item>
              <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" class="login-button" @click="handleLogin" :loading="loading"> 登 录</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-card>
    <div class="footer">Copyright © 2024 苏州同启苏沐软件有限公司 All Rights Reserved.</div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { Lock, User } from '@element-plus/icons-vue';

import { useUserStore } from '@/store/modules/user';
import { LoginData } from '@/api/types';
import { to } from 'await-to-js';

const userStore = useUserStore();
const router = useRouter();

const loginForm = ref<LoginData>({
  tenantId: '',
  username: '',
  password: '',
  rememberMe: false,
  code: '',
  uuid: ''
} as LoginData);

const loginRules: ElFormRules = {
  tenantId: [{ required: true, trigger: 'blur', message: '请输入您的租户编号' }],
  username: [{ required: true, trigger: 'blur', message: '请输入您的账号' }],
  password: [{ required: true, trigger: 'blur', message: '请输入您的密码' }],
  code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
};

const loading = ref(false);
// 验证码开关
const captchaEnabled = ref(true);

const redirect = ref(undefined);
const loginRef = ref<ElFormInstance>();

watch(
  () => router.currentRoute.value,
  (newRoute: any) => {
    redirect.value = newRoute.query && newRoute.query.redirect;
  },
  { immediate: true }
);

const handleLogin = () => {
  loginRef.value?.validate(async (valid: boolean, fields: any) => {
    if (valid) {
      loading.value = true;
      // 勾选了需要记住密码设置在 localStorage 中设置记住用户名和密码
      if (loginForm.value.rememberMe) {
        localStorage.setItem('tenantId', String(loginForm.value.tenantId));
        localStorage.setItem('username', String(loginForm.value.username));
        localStorage.setItem('password', String(loginForm.value.password));
        localStorage.setItem('rememberMe', String(loginForm.value.rememberMe));
      } else {
        // 否则移除
        localStorage.removeItem('tenantId');
        localStorage.removeItem('username');
        localStorage.removeItem('password');
        localStorage.removeItem('rememberMe');
      }
      // 调用action的登录方法
      const [err] = await to(userStore.login(loginForm.value));
      if (!err) {
        const redirectUrl = redirect.value || '/';
        await router.push(redirectUrl);
        loading.value = false;
      } else {
        loading.value = false;
        // 重新获取验证码
        if (captchaEnabled.value) {
        }
      }
    } else {
      console.log('error submit!', fields);
    }
  });
};

const getLoginData = () => {
  const tenantId = localStorage.getItem('tenantId');
  const username = localStorage.getItem('username');
  const password = localStorage.getItem('password');
  const rememberMe = localStorage.getItem('rememberMe');
  loginForm.value = {
    tenantId: tenantId === null ? String(loginForm.value.tenantId) : tenantId,
    username: username === null ? String(loginForm.value.username) : username,
    password: password === null ? String(loginForm.value.password) : String(password),
    rememberMe: rememberMe === null ? false : Boolean(rememberMe)
  } as LoginData;
};

onMounted(() => {
  getLoginData();
});
</script>

<style scoped lang="scss">
@keyframes gradient {
  0% {
    background-position: 0 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0 50%;
  }
}

@keyframes float {
  0% {
    transform: translateY(0) translateZ(0); /* 启用 GPU 加速 */
  }
  50% {
    transform: translateY(-10px) translateZ(0); /* 启用 GPU 加速 */
  }
  100% {
    transform: translateY(0) translateZ(0); /* 启用 GPU 加速 */
  }
}

@keyframes glow {
  0% {
    box-shadow: 0 0 20px rgba(255, 255, 255, 0.3);
  }
  50% {
    box-shadow: 0 0 40px rgba(255, 255, 255, 0.6);
  }
  100% {
    box-shadow: 0 0 20px rgba(255, 255, 255, 0.3);
  }
}

.login-wrapper {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: linear-gradient(-45deg, #b0c4de, #87cefa, #6495ed, #f0f8ff);
  background-size: 400% 400%;
  animation: gradient 10s ease infinite;
  position: relative;
  overflow: hidden;
}

.login-card {
  width: 720px;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(10px);
  transition:
    transform 0.3s ease,
    box-shadow 0.3s ease;
  animation: float 4s ease-in-out infinite;
  transform: translateZ(0); /* 启用 GPU 加速 */
  backface-visibility: hidden; /* 优化渲染 */
  perspective: 1000px; /* 优化渲染 */

  &:hover {
    transform: scale(1.00000001) translateZ(0); /* 启用 GPU 加速 */
    box-shadow: 0 15px 40px rgba(0, 0, 0, 0.3);
    animation: glow 2s ease-in-out infinite;
  }
}

.login-content {
  display: flex;
  min-height: 450px;
}

.login-left {
  flex: 1;
  background: rgb(49, 135, 246);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 50px;
}

.login-image {
  width: 100%;
  max-width: 300px;
  object-fit: contain;
  transition: transform 0.3s ease;
}

.login-right {
  flex: 1;
  padding: 50px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.login-title {
  font-size: 28px;
  margin-bottom: 40px;
  text-align: center;
  font-weight: bold;
  font-family: 'Poppins', sans-serif;
  background: linear-gradient(to right, #19e6bd, #00bfff, #1e90ff, #1969ef);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  -webkit-font-smoothing: antialiased; /* 改善字体抗锯齿 */
  -moz-osx-font-smoothing: grayscale; /* 改善字体抗锯齿 */
  transform: translateZ(0); /* 启用 GPU 加速 */
  backface-visibility: hidden; /* 优化渲染 */
  perspective: 1000px; /* 优化渲染 */
}

.login-form {
  width: 100%;
  max-width: 350px;
  margin: 0 auto;
}

.login-button {
  width: 100%;
  height: 45px;
  background: linear-gradient(135deg, #4086f4, #8470ff);
  border: none;
  color: #fff;
  font-size: 16px;
  font-weight: bold;
  margin-top: 20px;
  transition:
    transform 0.3s ease,
    box-shadow 0.3s ease;

  &:hover {
    transform: scale(1.00000001) translateZ(0); /* 启用 GPU 加速 */
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
  }
}

.footer {
  color: #fff;
  font-size: 14px;
  text-align: center;
  position: absolute;
  bottom: 20px;
  left: 0;
  right: 0;
}

:deep(.el-input__wrapper) {
  border-radius: 20px;
  padding: 5px 10px;
  transition:
    transform 0.3s ease,
    box-shadow 0.3s ease;

  &:hover {
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
  }
}

:deep(.el-button) {
  border-radius: 20px;
}
</style>
